package chain_user_config

import (
	"chain_bridge_service/internal/code"
	"chain_bridge_service/internal/pkg/core"
	"chain_bridge_service/internal/pkg/validation"
	chainConfig "chain_bridge_service/internal/repository/mongodb/chain_config"
	"chain_bridge_service/internal/repository/mongodb/user"
	"chain_bridge_service/internal/services/chain_user_config"
	"chain_bridge_service/internal/tools"
	"encoding/json"
	"github.com/ethereum/go-ethereum/common"
	"net/http"
)

type createRequest struct {
	CollectAddress string `bson:"collectAddress" json:"collectAddress"`
	FeeAddress     string `bson:"feeAddress" json:"feeAddress"`
	ChainConfigId  string `bson:"chainConfigId" json:"chainConfigId"`
}

type createResponse struct {
	Code int         `json:"code"`
	Data interface{} `json:"data"`
	Msg  string      `json:"msg"`
}

type AddressData struct {
	Code int `json:"code"`
	Data struct {
		Address string `json:"address"`
		Id      string `json:"id"`
	} `json:"data"`
	Msg string `json:"msg"`
}

func (h *handler) Create() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(createRequest)
		res := new(createResponse)

		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.ParamBindError,
				validation.Error(err)),
			)
			return
		}
		chainModel := chainConfig.NewModel()
		chainConfigInfo, err := chainModel.GetById(h.db.GetDb(), req.ChainConfigId)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.GetConfigError,
				code.Text(code.GetConfigError)).WithError(err),
			)
			return
		}

		createData := new(chain_user_config.CreateChainData)

		//createData.FeeAddress, createData.FeePrivateKey, err = tools.EncryptionKey(chainConfigInfo.ChainName)
		//if err != nil {
		//	ctx.AbortWithError(core.Error(
		//		http.StatusOK,
		//		code.ParamBindError,
		//		code.Text(code.ParamBindError)).WithError(err),
		//	)
		//	return
		//}

		var flag string
		if chainConfigInfo.ChainName == "TRX" {
			flag = "TRX"
		} else {
			flag = "EVM"
		}

		url := "http://192.168.0.188:8888/api/generateAddress?flag=" + flag
		body, err := tools.SendGet(url)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.SendGetError,
				code.Text(code.SendGetError)).WithError(err),
			)
			return
		}
		var data AddressData
		err = json.Unmarshal(body, &data)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.JsonError,
				code.Text(code.JsonError)).WithError(err),
			)
			return
		}
		if data.Code != 200 {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.CodeError,
				code.Text(code.CodeError)).WithError(err),
			)
			return
		}

		userInfo := ctx.GetInfo("userInfo").(*user.User)

		createData.UserId = userInfo.Id
		createData.CollectAddress = common.HexToAddress(req.CollectAddress).Hex()
		createData.FeeAddress = data.Data.Address
		createData.BusinessId = data.Data.Id
		createData.ChainName = chainConfigInfo.ChainName
		createData.ChainId = chainConfigInfo.ChainID
		createData.ChainConfigId = req.ChainConfigId
		err = h.chainUserConfigService.Create(ctx, createData)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.ProductCreateError,
				code.Text(code.ProductCreateError)).WithError(err),
			)
			return
		}

		res.Code = code.StatusOK
		res.Data = nil
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)
	}
}
